package com.ctshk.common.utils;

import com.ctshk.common.req.ScenicAddReq;
import com.ctshk.common.utils.individual.scenic.PlaceUrlDetailDto;
import com.ctshk.common.utils.individual.scenic.ScenicInfoDetailDto;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class ScenicSpotDetailUtill {
    private static String URL="http://travel.qunar.com/place/?from=header";
    private static String url="http://travel.qunar.com/p-cs299914-beijing-jingdian";
    private static String url1="http://travel.qunar.com/p-oi711995-tiananmenguangchang";
    public static void main(String[] args) throws Exception {
         getDestination(URL);//获取目的地url
//        List<String> urlList=getInfoUrl( url, "北京");//获取景点url集合

//        getInfoDetail( url1,"北京");
//
//        int count=0;
////        List<String> urlList=getInfoUrl( url, "北京");//获取景点url集合
//        for(String s : urlList){
//            //查询信息频率
////            try {
////                Thread.sleep(1000);
////            } catch (InterruptedException e) {
////                e.printStackTrace();
////            }
//            getInfoDetail( s,"北京");
//            count++;
//            System.out.println("count==========="+count);
//        }

//        generateTrainSet();

//        see();
    }

    /**
     * 获取目的地，链接
     * @param URL
     * @throws IOException
     */
    public static void getDestination(String URL) throws IOException {
        //通过延迟2000毫秒然后再去请求可解决js异步加载获取不到数据的问题
        Document doc= Jsoup.connect(URL).timeout(50000).get();
        // 通过class的名字得到（即XX）,一个数组对象Elements里面有我们想要的数据,至于这个div的值呢你打开浏览器按下F12就知道了;
//        Elements elements = doc.getElementsByClass("link");//contbox current
        Elements elements = doc.getElementsByClass("contbox current");
        System.out.println("Result---------------------start:");
        // Set<PlaceDto> set = Sets.newHashSet();
        List<PlaceUrlDetailDto> placeList=new LinkedList<PlaceUrlDetailDto>();

        for (int i = 0; i < elements.size(); ++i) {
            // 获取一个tr
            Element tr = elements.get(i);
            Elements td = tr.getElementsByClass("listbox");//获取区域
            for (Element q : td) {//获取第一个区域
                //获取名称
                Elements qy = q.getElementsByClass("hd");//获取区域
//                System.out.println(qy.text());
                //获取明细
                Elements sub_list = q.getElementsByClass("item");//获取目的地

                for (Element sub : sub_list) {
                    // 打印出每一个节点的信息;你可以选择性的保留你想要的数据,一般都是获取个固定的索引;
                    if (sub_list != null) {
                        PlaceUrlDetailDto place=new PlaceUrlDetailDto();

                        // System.out.println(sub.text());
                        place.setName(sub.text());
                        Element link= sub.select("a").first();
                        String url=link.attr("href");
                        //sub.getElementsByClass()
                        place.setSuperior(qy.text());
                        place.setFlag(""+i);
                        place.setUrl(url);
                        placeList.add(place);
                    }
                }
            }

            Elements tds = tr.getElementsByClass("hd");
            Elements t = tr.getElementsByClass("sub_list");

        }
        System.out.println(placeList);
        System.out.println("Result---------------------End");
        // System.out.println("Result---------------------"+name);



        //遍历目的地链接 获取景点链接
        for(PlaceUrlDetailDto placeList1 : placeList){
            String url1= placeList1.getUrl()+"-jingdian";
            System.out.println(url1);
            List<String> infoUrl = getInfoUrl(url1, placeList1.getName());

            for(String s : infoUrl){
//            //查询信息频率
////            try {
////                Thread.sleep(1000);
////            } catch (InterruptedException e) {
////                e.printStackTrace();
////            }
                try {
                    getInfoDetail( s,placeList1.getName());
                } catch (Exception e) {
                    e.printStackTrace();
                }
//            count++;
//            System.out.println("count==========="+count);
            }

        }
    }


    /**
     * 获取景点详情url
     * @param url1
     * @param name
     * @throws IOException
     */
    public static  List<String>   getInfoUrl(String url1,String name) throws IOException {
        Document doc1=Jsoup.connect(url1).timeout(50000).get();
        // 通过class的名字得到（即XX）,一个数组对象Elements里面有我们想要的数据,至于这个div的值呢你打开浏览器按下F12就知道了;
//        Elements elements = doc1.getElementsByClass("link");
        List<String> linkUrl=new LinkedList<>();

        Elements totalPages = doc1.getElementsByClass("page");
        int totalPage = Integer.valueOf(totalPages.get(totalPages.size()-2).text());
        for(int t = 0; t < totalPage; ++t){//遍历分页
            int param=t+1;
            url=url1+"-1-"+param;
            Document doc=Jsoup.connect(url).timeout(50000).get();
            Elements elements = doc.getElementsByClass("list_item clrfix");
            for (int i = 0; i < elements.size(); ++i) {
                //获取景点信息
                Element tr = elements.get(i);
                Elements item = tr.getElementsByClass("item");
                for(int j = 0; j < item.size(); ++j){
                    Element items = item.get(j);
                    //获取url
                    Elements titlink =  items.getElementsByClass("titlink");

                    for (Element links : titlink){
                        Element link= links.select("a").first();
                        String url=link.attr("href");
                        linkUrl.add(url);
                        //  getInfoDetail(url);
                    }

                    //System.out.println(scenic);

                }

            }

        }
        System.out.println("url++++++++++++++++++"+linkUrl);

        return  linkUrl;


    }

    /**
     * 获取景点信息
     * @param url
     * @throws IOException
     */
    public static void getInfoDetail(String url,String pName) throws Exception {
        //通过延迟然后再去请求可解决js异步加载获取不到数据的问题
        Document doc = Jsoup.connect(url).timeout(50000).get();
        // 通过class的名字得到（即XX）,一个数组对象Elements里面有我们想要的数据,至于这个div的值呢你打开浏览器按下F12就知道了;
        Elements elements = doc.getElementsByClass("b_title clrfix");
        ScenicInfoDetailDto scenic =new ScenicInfoDetailDto();
         while(elements==null || elements.size()<=0) {
              doc = Jsoup.connect(url).timeout(50000).get();
             // 通过class的名字得到（即XX）,一个数组对象Elements里面有我们想要的数据,至于这个div的值呢你打开浏览器按下F12就知道了;
              elements = doc.getElementsByClass("b_title clrfix");
              System.out.println("请输入验证码==========================");

         }
        getScenicInfo(elements, scenic, doc, pName,url);


    }


    /**
     * 获取景点信息
     * @param elements
     * @param scenic
     * @param doc
     * @param pName
     */
    public static void getScenicInfo(Elements elements, ScenicInfoDetailDto scenic, Document doc, String pName,String url)throws Exception{
        for (int i = 0; i < elements.size(); ++i) {
            String cn=null;
            String en=null;
            Element tr = elements.get(i);
            Elements item = tr.getElementsByClass("tit");
            for (int j = 0; j < item.size(); ++j) {
                Element items = item.get(j);
                cn = items.text(); //中文名
            }
            Elements entit = tr.getElementsByClass("entit");
            for (int j = 0; j < entit.size(); ++j) {
                Element entits = entit.get(j);
                en = entits.text();//英文名
                scenic.setEName(en);
            }

            String sb = cn;
            String str = sb.substring(0, sb.indexOf(en));
            scenic.setName(str);
            scenic.setPName(pName);
            //System.out.println(str);  //截取后的中文名
            //评分
            Elements scores = doc.getElementsByClass("score");
            String score = scores.get(0).text();
            scenic.setScore(score);
            //排名
            Elements ranking = doc.getElementsByClass("ranking");
            Elements sums = ranking.get(0).getElementsByClass("sum");
            String sum = sums.get(0).text();
            scenic.setRank(sum);

            //景点数量
            Elements counts = ranking.get(0).getElementsByClass("link");
            String count = counts.get(0).text();
            scenic.setCount(count);

            //游玩时间
            Elements scorebox = doc.getElementsByClass("m_scorebox");
            if(scorebox!=null && scorebox.size()>0) {
                Elements times = scorebox.get(0).getElementsByClass("time");
                if (times != null && times.size() > 0) {
                    String time = times.get(0).text();
                    scenic.setTourismSeason(time);
                }
            }


            //热评more_comment
            Elements comments = doc.getElementsByClass("more_comment");
            if(comments!=null && comments.size()>0){
                Elements txt = comments.get(0).getElementsByClass("txt");
                if(txt!=null && txt.size()>0) {
                    String comment = txt.get(0).text();

                }
            }

            //去过驴友占比countbox
            Elements countbox = doc.getElementsByClass("countbox");
            if(countbox!=null && countbox.size()>0) {
                Elements percents = countbox.get(0).getElementsByClass("percent");
                if(percents!=null && percents.size()>0) {
                    String percent = percents.get(0).text();
                    scenic.setProportion(percent);
                }
            }

            //攻略提到篇数

            if(countbox!=null && countbox.size()>0) {
                Elements strategys = countbox.get(0).getElementsByClass("sum");
                if(strategys!=null && strategys.size()>0) {
                    String strategy = strategys.get(0).text();
                    scenic.setStrategy(strategy);
                }
            }

            //概述

            Elements detail = doc.getElementsByClass("b_detail_section b_detail_summary");
            if(detail!=null && detail.size()>0) {
                Elements content_box = detail.get(0).getElementsByClass("e_db_content_box");
                if(content_box!=null && content_box.size()>0) {
                    String content = content_box.get(0).text();
                    scenic.setDes(content);
                }
            }

            //地址td_l
            Elements td_l = doc.getElementsByClass("td_l");
            if(td_l!=null && td_l.size()>0){
                Elements s = td_l.get(0).select("span");
                if(s.size()>0){
                    String address = s.get(0).text();
                    scenic.setAddress(address);
                }
                //电话
                if(s.size()>1) {
                    String phone = s.get(1).text();
                    scenic.setPhone(phone);
                }
            }


            //开放时间td_r
            Elements td_r = doc.getElementsByClass("td_r");
            if(td_l!=null && td_l.size()>0){
                Elements openTimes = td_r.get(0).select("span");
                if(openTimes!=null && openTimes.size()>0) {
                    String openTime = openTimes.get(0).text();
                    scenic.setTime(openTime);
                }
            }


            //旅游时节
            Elements travelseason = doc.getElementsByClass("b_detail_section b_detail_travelseason");
            if(travelseason!= null && travelseason.size()>0) {
                Elements p = travelseason.get(0).select("p");
                if(p!= null && p.size()>0) {
                    String tourismSeason = p.get(0).text();
                    scenic.setTourismSeason(tourismSeason);
                }

                //交通指南（地铁公交）e_db_content_box e_db_content_dont_indent
                Elements traffic = doc.getElementsByClass("b_detail_section b_detail_traffic");
                if(traffic!= null && traffic.size()>0) {
                    Elements indents = traffic.get(0).getElementsByClass("e_db_content_box e_db_content_dont_indent");
                    if(indents!= null && indents.size()>0) {
                        String indent = indents.get(0).text();
                        scenic.setBus(indent);
                    }
                }
            }

            //图片链接e_slider_con_box
            Document docpic = Jsoup.connect(url).timeout(50000).get();
            //http://travel.qunar.com/p-oi710603-gugong  //btnbox e_strategy
            //发送请求更多图片http://travel.qunar.com/place/api/poi/image?offset=0&limit=16&poiId=711995
            Elements pois = docpic.getElementsByClass("btnbox e_strategy");
            if(pois!= null && pois.size()>0) {//判断是否有10张图片，是否输验证码

                Element po= pois.get(0).select("a").first();
                String poii = po.attr("data-id");
                //发送更多图片请求
                String morePicUrl="http://travel.qunar.com/place/api/poi/image?offset=0&limit=16&poiId="+poii;//请求路径
                String picUrls= sendGet(morePicUrl);//从链接中取10张大图链接
                scenic.setPicUrl(picUrls);
            }else {//判断是否要验证码
                while(elements==null || elements.size()<=0) {
                    doc = Jsoup.connect(url).timeout(50000).get();
                    // 通过class的名字得到（即XX）,一个数组对象Elements里面有我们想要的数据,至于这个div的值呢你打开浏览器按下F12就知道了;
                    elements = doc.getElementsByClass("b_title clrfix");
                    System.out.println("请输入验证码==========================");
                }

            }

            ScenicAddReq addReq = new ScenicAddReq();
            addReq.setName(scenic.getName());

            //todo 这里需要把 ScenicInfoDetailDto 转成 ScenicAddReq，然后调用下面这个文档里的接口

            //todo http://127.0.0.1:9000/doc.html#/%E7%B3%BB%E7%BB%9F%E6%9C%8D%E5%8A%A1/%E6%99%AF%E7%82%B9%E8%B5%84%E6%96%99%E4%B8%BB%E6%95%B0%E6%8D%AE/addUsingPOST_20


            System.out.println("景点详情++++++++++++++++++"+scenic);
        }
    }



    public static  String   sendGet(String url){
          String picUrl=null;

        try {
          String datas = HttpUtils.sendHttpSslPost(url,"");
            JSONObject json = JSONObject.fromObject(datas);
            JSONArray dataDetail= (JSONArray)json.get("data");
             for(int i=0;i<10;++i) {
                 if (dataDetail != null && dataDetail.size()>10) {

                     if(dataDetail.get(i)!=null ){
                         Object o = dataDetail.get(i);
                         Map entry = (Map) o;
                         Object d = entry.get("bigImageURL");
                         if (i > 0) {
                             picUrl = picUrl + "," + d.toString();
                         } else if (i == 0) {
                             picUrl = d.toString();

                         }
                     }

             }

             }

        } catch (Exception e) {
            e.printStackTrace();
        }
   return picUrl;

    }
    /**
     * 阿里校验验证码
     * @param url
     * @return
     */
    public static  String  getVcode(String url){
        // url="http://captcha1.pbs.qunar.com/api/image?k={g3njj(t";

        //API产品路径VERIFY_CODE_ENTITY
        String requestUrl = "https://codevirify.market.alicloudapi.com/icredit_ai_image/verify_code/v1";
        //阿里云APPCODE
        String appcode = "4fcfc0082ed34e4eb24f7862b9489d7a";

        CloseableHttpClient httpClient = null;
        String result =null;
        try{
            httpClient = HttpClients.createDefault();
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            // 装填参数
            if (false) {

            } else {
                //启用URL方式进行识别
                //内容数据类型是图像文件URL链接
                params.add(new BasicNameValuePair("IMAGE", ""+url));
                params.add(new BasicNameValuePair("IMAGE_TYPE", "1"));
            }

            // 创建一个HttpGet实例
            HttpPost httpPost = new HttpPost(requestUrl);
            httpPost.addHeader("Authorization","APPCODE " + appcode);
            httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

            // 设置请求参数
            httpPost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));

            // 发送GET请求
            HttpResponse execute = httpClient.execute(httpPost);

            // 获取状态码
            int statusCode = execute.getStatusLine().getStatusCode();


            // 获取结果
            HttpEntity entity = execute.getEntity();
            result = EntityUtils.toString(entity, "utf-8");


        }catch (Exception e) {
            e.printStackTrace();
        }finally{
            if (httpClient != null) {
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    /**
     * 网络图片下载
     * @param imageUrl 图片url
     * @param formatName 文件格式名称
     * @param localFile 下载到本地文件
     * @return 下载是否成功
     */
    public static boolean downloadImage(String imageUrl, String formatName, File localFile) {
        boolean isSuccess = false;
        java.net.URL url = null;
        try {
            url = new URL(imageUrl);
            isSuccess = ImageIO.write(ImageIO.read(url), formatName, localFile);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return isSuccess;
    }

    public static void see(){
        ITesseract instance = new Tesseract();

        // 设置识别语言

        instance.setDatapath("D:\\Download");
        instance.setLanguage("eng");

        // 设置识别引擎

        instance.setOcrEngineMode(1);

        // 读取文件

        BufferedImage image = null;
        try {
            image = ImageIO.read(new File("D:\\Resources\\gzl\\途牛训练集\\3PFD.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {

            // 识别

            String result = instance.doOCR(image);
            System.out.println("识别结果："+result);
        } catch (TesseractException e) {
            System.err.println(e.getMessage());
        }

    }




}
